import java.util.Stack;

public class Solution591 {
    public boolean isValid(String code) {
        if (!code.startsWith("<")){
            return false;
        }
        int cur=0;
        Stack<String> stringStack=new Stack<>();
        while (cur<code.length()){
            if (code.charAt(cur)=='<'){
                if (cur+1>=code.length()){
                    return false;
                }
                cur++;
                if (code.charAt(cur)=='/'){
                    cur++;
                    int start=cur;
                    while (cur<code.length() && code.charAt(cur)!='>'){
                        cur++;
                    }
                    if (cur==code.length()){
                        return false;
                    }
                    String temp=code.substring(start,cur);
                    if (stringStack.isEmpty() || !stringStack.pop().equals(temp)){
                        return false;
                    }
                    cur++;
                    if (stringStack.isEmpty() && cur!=code.length()){
                        return false;
                    }
                }else if (code.charAt(cur)=='!'){
                    if (cur+7>=code.length()){
                        return false;
                    }
                    cur++;
                    String temp=code.substring(cur,cur+7);
                    if (!temp.equals("[CDATA[")){
                        return false;
                    }
                    cur=cur+7;
                    while (cur+2<code.length()){
                        String t=code.substring(cur,cur+3);
                        if (t.equals("]]>")){
                            break;
                        }
                        cur++;
                    }
                    if (cur+2==code.length()){
                        return false;
                    }
                    if (stringStack.isEmpty()){
                        return false;
                    }
                    cur=cur+3;
                }else{
                    int start=cur;
                    while (cur<code.length() && code.charAt(cur)!='>'){
                        cur++;
                    }
                    if (cur==code.length()){
                        return false;
                    }
                    String temp=code.substring(start,cur);
                    for (char c : temp.toCharArray()) {
                        if (!(c>='A' && c<='Z')){
                            return false;
                        }
                    }
                    if (temp.length()==0 || temp.length()>9){
                        return false;
                    }
                    stringStack.push(temp);
                    cur++;
                }
            }else {
                cur++;
            }
        }
        return stringStack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(new Solution591().isValid("<DIV>This is the first line <![CDATA[<div>]]></DIV>"));
    }
}
